package com.android.lovely.ui.dialog

import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import com.android.lovely.BuildConfig
import com.android.lovely.R
import com.android.lovely.databinding.DialogAdultBinding
import com.android.lovely.ext.startWebView
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.manager.KVStore
import com.kotlin.extensions.view.ViewExt.clickable
import kotlin.system.exitProcess

/**
 *
 * @author why
 * @date 2025/4/2 18:27
 */
class AdultDialog: BaseDialogFragment<DialogAdultBinding>() {
    override fun initializeViews() {
        binding.ivTopClose.clickable {
            exitProcess(0)
        }
        binding.confirm.clickable {
            KVStore.agreeLimit = true
            dismiss()
        }
        binding.content.movementMethod = LinkMovementMethod.getInstance()
        binding.content.setText(initAgreement(),TextView.BufferType.SPANNABLE)
    }

    private fun initAgreement(): CharSequence {
        val agreement = getString(R.string.login_provice)
        try {
            val spaBuilder = SpannableStringBuilder(agreement)
            val privacySpan = getString(R.string.login_privacy_agreement)
            val serviceSpan = getString(R.string.login_user_agreement)
            spaBuilder.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        (widget as TextView).highlightColor =
                            resources.getColor(R.color.transparent)
                        requireActivity().startWebView(
                            getString(R.string.login_privacy_agreement),
                            BuildConfig.TERM
                        )
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = resources.getColor(R.color.color_main)
                        ds.isUnderlineText = false
                        ds.clearShadowLayer()
                    }
                },
                spaBuilder.indexOf(privacySpan),
                spaBuilder.indexOf(privacySpan) + privacySpan.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            spaBuilder.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        (widget as TextView).highlightColor =
                            resources.getColor(R.color.transparent)
                        requireActivity().startWebView(
                            getString(R.string.login_user_agreement),
                            BuildConfig.PRIVACY
                        )
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = resources.getColor(R.color.color_main)
                        ds.isUnderlineText = false
                        ds.clearShadowLayer()
                    }
                },
                spaBuilder.indexOf(serviceSpan),
                spaBuilder.indexOf(serviceSpan) + serviceSpan.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            return spaBuilder
        } catch (e: Exception) {
            return agreement
        }
    }


    override fun getWindowHeight(): Int {
        return WindowManager.LayoutParams.MATCH_PARENT
    }

}